package com.example.hezhe_backend.controller;

import com.example.hezhe_common.utils.SMSUtils;
import com.example.hezhe_common.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
public class SendCodeController {

    @Autowired
    private JedisPool jedisPool;

    //将yml相关短信服务配置注入
    @Value("${tencent.dx.secretId}")
    private String secretId;
    @Value("${tencent.dx.secretKey}")
    private String secretKey;
    @Value("${tencent.dx.sdkAppId}")
    private String sdkAppId;
    @Value("${tencent.dx.signName}")
    private String signName;
    @Value("${tencent.dx.templateId}")
    private String templateId;

    //发送验证码
    @RequestMapping("/sendCode")
    public String sendCode(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        try{
            SMSUtils.sendShortMessage(secretId,secretKey,sdkAppId,signName,templateId,telephone,code.toString());
//            System.out.println(secretId);
//            System.out.println(secretKey);
//            System.out.println(sdkAppId);
//            System.out.println(signName);
//            System.out.println(templateId);
//            System.out.println(telephone);
//            System.out.println(code);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        jedisPool.getResource().setex(telephone,60*5,code.toString());
        return code.toString();
    }

    //发送忘记验证码密码
    @RequestMapping("/sendFRTCode")
    public String sendFRTCode(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        try{
            SMSUtils.sendShortMessage(secretId,secretKey,sdkAppId,signName,templateId,telephone,code.toString());

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        jedisPool.getResource().setex(telephone+"FRT",60*5,code.toString());
        return code.toString();
    }

}
